﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using System.Threading.Tasks;
namespace LeecodeTest
{


    public class Leecode : MonoBehaviour
    {

        Queue<char> str = new Queue<char>();

        private void Start()
        {
            //            var test = new code1470();
            //            Debug.Log(test.startTest(3,2,5,1,3,4,7));
            //            int test = 10;
            //            Debug.Log($"当前值{test} 二进制{ Convert.ToString(test, 2)}");
            //            test |= 20 << 5;
            //            //            test |= 1 << 4;
            //            //            test ^= 1 << 5;
            //            Debug.Log($"当前值{test} 二进制{ Convert.ToString(test, 2)}");
            //            test = test >> 5;
            //            Debug.Log($"当前值{test} 二进制{ Convert.ToString(test, 2)}");

            int test = 12556223;
//            Debug.Log(Mathf.Pow(2, 32));
            Debug.Log($"当前值{test} 二进制{ Convert.ToString(test, 2)}");

            test &= 1023;
            Debug.Log($"当前值{test} 二进制{ Convert.ToString(test, 2)}");

        }

        #region
        private ListNode NodeList(params int[] value)
        {
            var node = new ListNode(-1);
            var per = node;
            for (int i = 0; i < value.Length; i++)
            {
                per.next = new ListNode(value[i]);
                per = per.next;
            }
            return node.next;
        }



  
        Dictionary<char, int> map = new Dictionary<char, int>();
        int leftIndex = 0;
        int count = 0;
        public int LengthOfLongestSubstring(string s)
        {
            leftIndex = 0;
            count = 0;
            map.Clear();
            for (int i = 0; i < s.Length; i++)
            {
                if (map.ContainsKey(s[i]))
                {
                    //若出现重复值 移动起始下标到重复值的下一位
                    leftIndex = leftIndex > map[s[i]] + 1 ? leftIndex : map[s[i]] + 1;
                }
                map[s[i]] = i;
                count = count > i - leftIndex + 1 ? count : i - leftIndex + 1;
            }
            return count;
        }
        #endregion
        public double FindMedianSortedArrays(int[] nums1, int[] nums2)
        {
            int[] combine = new int[nums1.Length + nums2.Length];
            int count = combine.Length;
            int currnum1 = 0;
            int currnum2 = 0;

            for (int i = 0; i < count; i++)
            {

                if (currnum1 >= nums1.Length)
                {
                    while (currnum2 < nums2.Length)
                    {
                        if (combine[Math.Max(0, i - 1)] == nums2[currnum2])
                        {
                            currnum2 += 1;
                            count -= 1;
                            continue;
                        }
                        combine[i] = nums2[currnum2];
                        i += 1;
                        currnum2 += 1;
                    }
                    break;
                }
                if (currnum2 >= nums2.Length)
                {
                    while (currnum1 < nums1.Length)
                    {

                        if (combine[Math.Max(0, i - 1)] == nums1[currnum1])
                        {
                            currnum1 += 1;
                            count -= 1;
                            continue;
                        }
                        combine[i] = nums1[currnum1];
                        i += 1;
                        currnum1 += 1;

                    }
                    break;
                }

                if (nums1[currnum1] > nums2[currnum2])
                {
                    if (nums1[currnum1] != combine[Math.Max(0, i - 1)])
                    {
                        combine[i] = nums2[currnum2];
                    }
                    else
                    {
                        i -= 1;
                        count -= 1;
                    }
                    currnum2 += 1;
                }
                else if (nums1[currnum1] < nums2[currnum2])
                {
                    if (nums1[currnum1] != combine[Math.Max(0, i - 1)])
                    {
                        combine[i] = nums1[currnum1];
                    }
                    else
                    {
                        i -= 1;
                        count -= 1;
                    }
                    currnum1 += 1;
                }
                else if (nums2[currnum2] == nums1[currnum1])
                {
                    if (nums1[currnum1] != combine[Math.Max(0, i - 1)])
                    {
                        combine[i] = nums1[currnum1];
                    }
                    else
                    {
                        //                    combine[i] = 0;

                        count -= 1;
                        i -= 1;
                    }
                    //                combine[i] = nums2[currnum2] ;
                    currnum2 += 1;
                    currnum1 += 1;
                    count -= 1;
                }
            }

            Debug.Log(count);
            string str = null;
            foreach (var item in combine)
            {
                str += item.ToString() + " ";
            }
            Debug.Log(str);

            if (count == 0) return 0;
            if (count % 2 == 0)
            {
                return (combine[count / 2] + combine[count / 2 - 1]) * 0.5d;
            }
            else
            {
                return combine[count / 2];
            }
        }

        /// <summary>
        /// 1480
        /// </summary>
        /// <param name="nums"></param>
        /// <returns></returns>
        public int[] RunningSum(int[] nums)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                if (i > 0)
                {
                    nums[i] += nums[i - 1];
                }
            }
            return nums;
        }

        public ListNode InsertionSortList(ListNode head)
        {
            var dummyHead = new ListNode(0);
            dummyHead.next = head;
            var last = head;
            var curr = head.next;
            //当前指向于排序最后一位指向对比
            while (curr != null)
            {
                if (last.val >= curr.val)
                {
                    var per = dummyHead.next;
                    while (per.next.val <= curr.val)
                    {
                        per = per.next;
                    }
                    last.next = curr.next;
                    curr.next = per.next;
                    per.next = curr;
                    if (curr.val < dummyHead.next.val)
                    {
                        dummyHead.next = curr;
                    }
                }
                else
                {
                    last = last.next;
                }

                curr = last.next;
            }
            return dummyHead.next;
        }
    }


    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int x) { val = x; }
    }
}
